package com.gulimall.product.service.impl;

import com.alibaba.cloud.commons.lang.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.google.common.base.Strings;
import com.gulimall.product.dao.AttrAttrgroupRelationDao;
import com.gulimall.product.entity.AttrAttrgroupRelationEntity;
import com.gulimall.product.entity.AttrEntity;
import com.gulimall.product.service.AttrService;
import com.gulimall.product.vo.AttrGroupRelationVo;
import com.gulimall.product.vo.AttrGroupWIthAttrsVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gulimall.common.utils.PageUtils;
import com.gulimall.common.utils.Query;

import com.gulimall.product.dao.AttrGroupDao;
import com.gulimall.product.entity.AttrGroupEntity;
import com.gulimall.product.service.AttrGroupService;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Autowired
    private AttrAttrgroupRelationDao relationDao;

    @Autowired
    private AttrService attrService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 分页查询分类属性分组
     *
     * @param params
     * @param catelogId
     * @return
     */
    @Override
    public PageUtils queryPage(Map<String, Object> params, Long catelogId) {
        //设置查询条件
        // select * from pms_attr_group where (attr_group_id = key or attr_group_name like key)
        LambdaQueryWrapper<AttrGroupEntity> queryWrapper = Wrappers.lambdaQuery(AttrGroupEntity.class);
        String key = (String) params.get("key");
        if (StringUtils.isNotEmpty(key)){
            queryWrapper.and(obj -> {
                obj.eq(AttrGroupEntity::getAttrGroupId, key).or().like(AttrGroupEntity::getAttrGroupName, key);
            });
        }


        //分类id = 0，表示查询所有数据
        if (catelogId == 0) {
            //使用renren封装的分页工具
            IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params), queryWrapper);
            return new PageUtils(page);
        }
        //id不为0，查询指定id的数据
        // select * from pms_attr_group where (attr_group_id = key or attr_group_name like key) and catelog_id = catelogId
        queryWrapper.eq(AttrGroupEntity::getCatelogId, catelogId);
        IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params), queryWrapper);
        return new PageUtils(page);
    }

    /**
     * 批量删除分组关联的属性
     * @param relationIds
     */
    @Override
    public void deleteAttrGroupRelations(List<AttrGroupRelationVo> relationIds) {
        List<AttrAttrgroupRelationEntity> relationEntityList = relationIds.stream().map(item -> {
            AttrAttrgroupRelationEntity relationEntity = new AttrAttrgroupRelationEntity();
            BeanUtils.copyProperties(item, relationEntity);
            return relationEntity;
        }).collect(Collectors.toList());

        relationDao.deleteRelations(relationEntityList);
    }

    @Override
    public List<AttrGroupWIthAttrsVo> getAttrGroupWithAttrsByCatelogId(Long catelogId) {

        //1.获取当前分类下的所有分组
        LambdaQueryWrapper<AttrGroupEntity> queryWrapper = Wrappers.lambdaQuery();
        if (catelogId != null){
            queryWrapper.eq(AttrGroupEntity::getCatelogId,catelogId);
        }
        List<AttrGroupEntity> attrGroupEntityList = this.list(queryWrapper);

        return attrGroupEntityList.stream().map(attrGroupEntity -> {
            AttrGroupWIthAttrsVo attrGroupWIthAttrsVo = new AttrGroupWIthAttrsVo();
            BeanUtils.copyProperties(attrGroupEntity, attrGroupWIthAttrsVo);

            List<AttrEntity> attrs = attrService.getAttrGroupRelationById(attrGroupEntity.getAttrGroupId());
            attrGroupWIthAttrsVo.setAttrs(attrs);

            return attrGroupWIthAttrsVo;
        }).collect(Collectors.toList());


    }

}